package com.xh.easyretry.strategy.time;

import com.xh.easyretry.annotations.TimeAvoidance;
import com.xh.easyretry.annotations.XHRetry;
import com.xh.easyretry.common.XHRetryContext;
import com.xh.easyretry.common.XHTimeAvoidanceContext;
import org.springframework.stereotype.Component;

import static com.xh.easyretry.common.XHRetryConstants.IMMEDIATE_STRATEGY;

@Component
public class ImmediateTimeAvoidanceStrategy implements TimeAvoidanceStrategy {

    @Override
    public String getStrategy() {
        return IMMEDIATE_STRATEGY;
    }

    @Override
    public XHTimeAvoidanceContext start(XHRetryContext retryContext) {
        XHRetry xhRetry = retryContext.getXhRetry();
        TimeAvoidance timeAvoidance = xhRetry.timeAvoidance();
        XHTimeAvoidanceContext timeAvoidanceContext = new XHTimeAvoidanceContext(timeAvoidance, null,
                null, null, null);
        retryContext.setTimeAvoidanceContext(timeAvoidanceContext);
        return timeAvoidanceContext;
    }

    @Override
    public long calculateDelayTime(XHRetryContext retryContext) {
        XHRetry xhRetry = retryContext.getXhRetry();
        TimeAvoidance timeAvoidance = xhRetry.timeAvoidance();
        return timeAvoidance.time();
    }
}
